package com.shou.domain.common;

import com.shou.types.common.TableNameEnum;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * ClassName:AbstractService
 * Project:SmartPark
 * Package: com.shou.domain.common
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/8 19:03
 * @Version 1.0
 */
public class AbstractService {
    @Resource
    IBaseRepository baseRepository;
    public void deleteByIds(List<Integer> ids, TableNameEnum tableNameEnum) {
        validExistByIds(ids, tableNameEnum);
        baseRepository.deleteByIds(ids, tableNameEnum.getEnTableName());
    }

    public void deleteAll(TableNameEnum tableNameEnum) {
        if(baseRepository.checkIfRecordsExist(tableNameEnum.getEnTableName())){
            baseRepository.deleteAllByTableName(tableNameEnum.getEnTableName());
        } else{
            throw new RuntimeException(tableNameEnum.getCnTableName() + "内无信息，无需清空！");
        }
    }
    /**
     * 根据ID列表检查规则是否存在于所在表
     * 用于批量操作，最大限制不超100条
     */
    protected void validExistByIds(List<Integer> ids, TableNameEnum tableNameEnum) {
        for (Integer id : ids) {
            if(!checkIfExistById(id, tableNameEnum))
                throw new RuntimeException("id: " + id + "对应的记录不存在,删除失败！");
        }
    }

    /**
     * 根据ID检查规则是否存在于所在表
     */
    protected boolean checkIfExistById(Integer id, TableNameEnum tableNameEnum) {
        return baseRepository.checkIfExistById(id, tableNameEnum.getEnTableName());
    }

    protected void validateCode(String code, String tableName, Integer... excludeIds) {
       if(checkIfExistByCode(code, tableName, excludeIds)){
           throw new RuntimeException("编号: " + code + "已存在，请重新输入！");
       }
    }

    protected boolean checkIfExistByCode(String code, String tableName, Integer... excludeIds) {
        return baseRepository.checkIfExistByCode(code, tableName, excludeIds);
    }

    /**
     * 检查查询参数合理性
     */
    protected void validateQueryParams(PageQueryEntity pageQueryEntity, TableNameEnum tableNameEnum) {
        // 检查排序参数
        validateSortParams(pageQueryEntity, tableNameEnum);
    }

    protected void validateSortParams(PageQueryEntity pageQueryEntity, TableNameEnum tableNameEnum) {
        String sortName = pageQueryEntity.getSortName();
        Integer sort = pageQueryEntity.getSort();

        if(!StringUtils.isBlank(sortName)){
            if(!getColumnsByEnTableName(tableNameEnum.getEnTableName()).contains(sortName)){
                throw new RuntimeException("排序字段不存在");
            }
            if(sort != null && !(sort == 1 || sort == 0)){
                throw new RuntimeException("排序参数错误,只能为0或1");
            }
        }
    }

    /**
     * 根据英文表名获取所有数据项名
     */
    private List<String> getColumnsByEnTableName(String enTableName) {
        return baseRepository.getColumnsByEnTableName(enTableName);
    }

    /**
     * 将驼峰命名法转换为下划线命名法
     * @param camelCase 驼峰命名法的字符串
     * @return 下划线命名法的字符串
     */
    public static String camelToSnake(String camelCase) {
        if (camelCase == null || camelCase.isEmpty()) {
            return camelCase;
        }
        StringBuilder snakeCase = new StringBuilder();
        for (char c : camelCase.toCharArray()) {
            if (Character.isUpperCase(c)) {
                if (snakeCase.length() > 0) {
                    snakeCase.append('_');
                }
                snakeCase.append(Character.toLowerCase(c));
            } else {
                snakeCase.append(c);
            }
        }
        return snakeCase.toString();
    }
}
